package com.xinbochuang.machine.admin.service.impl.macRoom;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.MacRoomResource;
import com.xinbochuang.machine.admin.domain.macRoom.EquCabinet;
import com.xinbochuang.machine.admin.domain.macRoom.MacShow;
import com.xinbochuang.machine.admin.domain.macRoom.json.DataNode;
import com.xinbochuang.machine.admin.domain.macRoom.json.Node;
import com.xinbochuang.machine.admin.mapper.macRoom.MacShowMapper;
import com.xinbochuang.machine.admin.service.IMacRoomResourceService;
import com.xinbochuang.machine.admin.service.macRoom.IEquCabinetService;
import com.xinbochuang.machine.admin.service.macRoom.IMacShowService;
import com.xinbochuang.machine.common.enums.MacResType;
import com.xinbochuang.machine.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 黄晓鹏
 * @date 2020-11-20
 */
@Service
public class MacShowServiceImpl extends ServiceImpl<MacShowMapper, MacShow> implements IMacShowService {

    @Resource
    private MacShowMapper macShowMapper;

    @Resource
    private IMacShowService macShowService;

    @Resource
    private IEquCabinetService cabinetService;

    @Resource
    private IMacRoomResourceService macRoomResourceService;

    /**
     * 根据机房id查询节点
     *
     * @param macId 机房id
     * @return 结果
     */
    @Override
    public List<MacShow> getNodes(String macId) {
        return macShowMapper.getNodes(macId);
    }

    /**
     * 保存节点数据
     *
     * @param dataNode 节点数据
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveNodes(DataNode dataNode) {
        // 删除旧数据
        macShowService.remove(Wrappers.<MacShow>lambdaQuery().eq(MacShow::getMacId, dataNode.getMacId()));

        // 删除旧机柜数据
        cabinetService.remove(Wrappers.<EquCabinet>lambdaQuery().eq(EquCabinet::getMacId, dataNode.getMacId()));

        // 删除资源旧数据
        macRoomResourceService.remove(Wrappers.<MacRoomResource>lambdaQuery().eq(MacRoomResource::getMacRoomId, dataNode.getMacId()));

        List<Node> nodes = dataNode.getNodes();
        // 保存节点数据
        List<MacShow> list = new ArrayList<>();
        List<EquCabinet> cabinetList = new ArrayList<>();

        List<MacRoomResource> resourceList = new ArrayList<>();
        for (Node node : nodes) {
            MacShow macShow = new MacShow();
            macShow.setId(node.getId());
            macShow.setMacId(node.getMacId());
            macShow.setNode(JSONUtil.toJsonStr(node));
            macShow.setCreateTime(DateUtil.now());

            // 机柜
            EquCabinet cabinet = new EquCabinet();
            cabinet.setUuid(IdUtil.simpleUUID());
            cabinet.setCabinetName(node.getCabinetName());
            cabinet.setMacId(node.getMacId());
            cabinet.setCreateBy(SecurityUtils.getUsername());
            cabinet.setCreateTime(DateUtil.now());
            if (StrUtil.isNotBlank(node.getUNum())) {
                cabinet.setUlocationQuantity(node.getUNum());
            }
            cabinetList.add(cabinet);

            macShow.setCabinetId(cabinet.getUuid());
            list.add(macShow);

            //添加到资源表
            MacResType macResType = MacResType.getEnum(node.getType());
            if (macResType != null) {
                MacRoomResource resource = new MacRoomResource();
                resource.setUuid(UUID.fastUUID().toString());
                resource.setDeleteFlag("0");
                resource.setMacRoomId(dataNode.getMacId());
                resource.setResourceType(macResType.getName());
                resource.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickname());
                resource.setCreateTime(DateUtil.now());
                resourceList.add(resource);
            }

        }
        // 保存机柜
        cabinetService.saveBatch(cabinetList);
        // 保存资源
        macRoomResourceService.saveBatch(resourceList);
        // 保存节点数据
        macShowService.saveBatch(list);
        return true;
    }
}
